//
//  WBStatuedViewModel.swift
//  weibo
//
//  Created by yanghong on 15/12/11.
//  Copyright © 2015年 杨鸿. All rights reserved.
//

import UIKit
// 在这里面处理WBStatues 的逻辑
class WBStatuedViewModel: NSObject {
    
    
    var status: WBStatues? {
        //外界给它设置值的时候才会调用 didSet
        didSet{
            
        }
        
    }
    // 设置VIP等级图标
    var vipImg: UIImage? {
        
        guard let mbrank = status?.user?.mbrank else {
            
            return nil
            
        }
        if mbrank > 0 && mbrank < 7 {
            
            return UIImage(named: "common_icon_membership_level\(mbrank)")
            
        }
        return nil
    }
    //设置认证图标
    var  verifiedImg: UIImage? {
        
        guard let verifiedImg = status?.user?.verified else {
            return nil
        }
        switch verifiedImg {
        case 1:
            return UIImage(named: "avatar_vip")
        case 2,3,5:
            return UIImage(named: "avatar_enterprise_vip")
        case 220:
            return UIImage(named: "avatar_grassroot")
            
        default:
            return nil
        }
    }
    
    var retweetCountString: String = "转发"
    var commentCountString: String = "评论"
    var attitudeCountString: String = "赞"
    //转发微博的内容
    var retweetText: String?
    //微博的来源
    var sourceText: String?
    //创建时间
    var creatAtText: String? {
    
        guard let creatDate = NSDate.sinaDate(status!.created_at!) else {
        
         return ""
        }
        
        return creatDate.sinaDateString
    }
    init(status: WBStatues) {
        
        super.init()
        
        self.status = status
        
        retweetCountString = getCountString((self.status?.reposts_count)!, defaultTitle: "转发")
        commentCountString = getCountString((self.status?.comments_count)!, defaultTitle: "评论")
        attitudeCountString = getCountString((self.status?.attitudes_count)!, defaultTitle: "赞")
        
        //转发内容的逻辑
        
        if let retweetStatus = status.retweeted_status {
        
            if let  retweetName = retweetStatus.user?.name {
            
               retweetText =  "@\(retweetName):\(retweetStatus.text!)"
            
            }

        }
        
        if let source = status.source where source.containsString("\">") {
            // 判断是否能够找到range
            let startRange = source.rangeOfString("\">")!
            let endRange = source.rangeOfString("</")!
            sourceText = "来自 \(source.substringWithRange(startRange.endIndex..<endRange.startIndex))"
        }
     
    }
    
    
    
    private func getCountString(count: Int, defaultTitle: String) -> String {
        
        if count > 0 {
            
            if count < 10000 {
                
                return "\(count)"
            }else{
                let result =  CGFloat(count / 1000)/10
                
                let resultString = "\(result)万"
                
                if resultString.containsString(".0") {
                    
                    return resultString.stringByReplacingOccurrencesOfString(".0", withString: "")
                    
                }
                
                return resultString
            }
        }
        return defaultTitle
    }
    
    override var description: String {
        
        let keys = ["status"]
        
        return dictionaryWithValuesForKeys(keys).description
        
    }
}
